// import archiver from "archiver";
import path from "path";
import ui from "./ui";
import sftp from "./sftp";
import config from "../package.json";
let deplayConfig = config.deploy;
let meta = ui.meta(import.meta);
let { __dirname, __filename } = meta;
console.log('deployConfig:', __dirname, __filename);

let compressFile = path.resolve(`${meta.cachedir}/dist.zip`);
let compressDir = path.resolve(`${meta.cwd}/${deplayConfig.deployDir}`);

async function main() {
    let { size } = await ui.zipdir(compressDir, compressFile);
    console.log('文件尺寸:', size);
    let upload = {
        file: compressFile,
        target: '/root/dist.zip',
    }, shell;
    shell = [
        `rm -rf ${upload.target}\n`,//删除谨慎操作
        `exit\n`
    ];
    console.log(shell);
    await sftp.shell(shell, false);
    await sftp.upload(upload.file, upload.target, false);
    console.log('上传完成:', upload);
    shell = [
        `rm -rf ${deplayConfig.targetDir}\n`,
        `mkdir ${deplayConfig.targetDir}\n`,
        `mv ${upload.target} ${deplayConfig.targetDir} \n`,
        `cd ${deplayConfig.targetDir}\n`,
        `unzip ./dist.zip\n`,
        `rm -rf ./dist.zip\n`,
        `chown www:www ./*\n`,
        `exit\n`
    ];
    console.log(shell);
    await sftp.shell(shell);
    // shell = [
    //     `ls -al ${deplayConfig.targetDir}\n`,
    //     `exit\n`
    // ];
    // let res = await sftp.shell(shell);
    // console.log(res);
}
main();

//demo upload
// async function shell() {
//     let { size } = await ui.zipdir(compressDir, compressFile);
//     console.log('文件尺寸:', size)
//     let upload = {
//         file: compressFile,
//         target: '/root/dist.zip',
//     }
//     await sftp.upload(upload.file, upload.target);
// }
// shell();


//demo shell
// async function shell() {
//     let shells = [
//         `cd /root\n`,
//         `pwd\n`,
//         `ls -al\n`,
//         `exit\n`
//     ];
//     let res = await sftp.shell(shells);
//     console.log('res', res);
// }
// shell();